/**
 * 
 */
package com.gwcloud.leetcode.offers;

import com.gwcloud.leetcode.common.datastructure.ListNode;

/**
 * @ClassName   : Leetcode0250 
 * @author      : ganwei 
 * @date        : 2020年6月2日
 * @Description : 面试题25. 合并两个排序的链表
 */
public class Leetcode0250 {
    /**
     * -解题思路，归并
     */
	public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    	ListNode newhead = null;
		ListNode head = null;
    	while(l1 != null && l2 != null){
    		if(l1.val <= l2.val) {
    			if(newhead == null) {
    				newhead = new ListNode(l1.val);
    				head = newhead;
    			}else {
    				newhead.next = new ListNode(l1.val);
    				newhead = newhead.next;
    			}
    			l1 = l1.next;
    		}else {
    			if(newhead == null) {
    				newhead = new ListNode(l2.val);
    				head = newhead;
    			}else {
    				newhead.next = new ListNode(l2.val);
    				newhead = newhead.next;
    			}
    			l2 = l2.next;
    		}
    	}
    	if(l1 != null) {
    		newhead.next = l1;
    	}
    	if(l2 != null) {
    		newhead.next = l2;
    	}
    	
    	return head;
    }
	
	/**
	 * -主函数，
	 */
	public static void main(String[] args) {
		
	}
}
